import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
import Users from '../menu/user.vue'
import Orders from '../menu/order.vue'
import Rights from '../menu/right.vue'
import Setting from '../menu/setting.vue'
import Email from '../menu/email.vue'
import message from '../menu/message.vue'
import Details from '../users/details.vue'
import Analysis from '../menu/analysis.vue'
Vue.use(VueRouter)

const router=new VueRouter({
    routes:[
        {path:'/',redirect:'login'},
        {path:'/login',component:Login,name:'Login'},
        {path:'/home',component:Home,name:'home',redirect:"/home/users",
        children:[
            {path:'users',component:Users},
            {path:'orders',component:Orders},
            {path:'settings',component:Setting},
            {path:'emails',component:Email},
            {path:'message',component:message},
            {path:'rights',component:Rights},
            {path:'Analysis',component:Analysis},
            {
                path:'users/:id',component:Details,props:true
            }
        ]}
    ]
})

router.beforeEach((to,from,next)=>{
    if(to.path==='/login') return next()
    const token=localStorage.getItem('token')
    if(!token){
      return  next('/login')
    }
    next()
})
export default router;